package com.howells.time.module.playvideo.view;

import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.howells.time.R;
import com.howells.time.base.utils.ToastShow;
import com.howells.time.module.UIHelper;
import com.howells.time.module.playvideo.model.CommentModel;
import com.howells.time.module.playvideo.presenter.VideoPresenter;
import com.howells.time.module.playvideo.presenter.VideoPresenterImp;
import com.yqritc.scalablevideoview.ScalableVideoView;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class VideoActivity extends AppCompatActivity implements VideoView,SwipeRefreshLayout.OnRefreshListener{

    public static final String TAG = "VideoActivity";

    private ScalableVideoView mScalableVideoView;
    private ImageView mPlayImageView;
    private ImageView mThumbnailImageView;
    private RadioButton isPrivate;
    private String filePath;

    private VideoPresenter mVideoPresenter;

    private SwipeRefreshLayout mSwipeRefreshLayout;
    private List<CommentModel> list;
    private CommentAdapter mCommontAdapter;
    private RecyclerView mListview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        //Toobar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.drawable.login_header_back_normal);
        toolbar.setTitle("小视频");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //悬浮球
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ToastShow.ToastShort(getBaseContext(),"评论");
                UIHelper.jumpComment(VideoActivity.this);
            }
        });

        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.commentswiprefresh);
        mSwipeRefreshLayout.setOnRefreshListener(this);

        mVideoPresenter=new VideoPresenterImp(this);
        init();
    }

    private void init() {
        filePath="";
        mScalableVideoView = (ScalableVideoView) findViewById(R.id.video_view);
        try {
            // 这个调用是为了初始化mediaplayer并让它能及时和surface绑定
            if (mScalableVideoView != null) {
                mScalableVideoView.setDataSource(filePath);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        isPrivate= (RadioButton) findViewById(R.id.radioButton);
        mPlayImageView = (ImageView) findViewById(R.id.playImageView);

        mThumbnailImageView = (ImageView) findViewById(R.id.thumbnailImageView);
        mThumbnailImageView.setImageBitmap(getVideoThumbnail(filePath));

        list=new LinkedList<>();
        adddata();
        mListview= (RecyclerView) findViewById(R.id.comment_recycler);
        mListview.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
        mCommontAdapter=new CommentAdapter(this,R.layout.item_message,list);
        mListview.setAdapter(mCommontAdapter);



    }

    private void adddata() {
        CommentModel sm=new CommentModel("好友1","2016-8-21","1111111111111111");
        list.add(sm);
        sm=new CommentModel("好友2","2016-4-22","2222222222222222222222222222222222222222");
        list.add(sm);
        sm=new CommentModel("好友3","2016-4-23","11111111111111113333333333333333333333333333333333333333333333333333");
        list.add(sm);
        sm=new CommentModel("好友4","2016-4-24","0");
        list.add(sm);
        sm=new CommentModel("好友5","2016-4-13","666666666666666666666666666666666666666666666666666666666666666");
        list.add(sm);
    }

    @Override
    public void onRefresh() {
        //异步调用刷新
        //messagePresenter.refresh(null);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(false);
                ToastShow.ToastShort(getBaseContext(),"刷新成功");
            }
        },1000);
    }

    /**
     * 获取视频缩略图（这里获取第一帧）
     * @param filePath
     * @return
     */
    public Bitmap getVideoThumbnail(String filePath) {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource(filePath);
            bitmap = retriever.getFrameAtTime(TimeUnit.MILLISECONDS.toMicros(1));
        }
        catch(IllegalArgumentException e) {
            e.printStackTrace();
        }
        finally {
            try {
                retriever.release();
            }
            catch (RuntimeException e) {
                e.printStackTrace();
            }
        }
        return bitmap;
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.video_view:
                mScalableVideoView.stop();
                mPlayImageView.setVisibility(View.VISIBLE);
                mThumbnailImageView.setVisibility(View.VISIBLE);
                break;
            case R.id.playImageView:
                try {
                    mScalableVideoView.setDataSource(filePath);
                    mScalableVideoView.setLooping(true);
                    mScalableVideoView.prepare();
                    mScalableVideoView.start();
                    mPlayImageView.setVisibility(View.GONE);
                    mThumbnailImageView.setVisibility(View.GONE);
                } catch (IOException e) {
                    Log.e(TAG, e.getLocalizedMessage());
                    Toast.makeText(this, "视频正在下载", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
